import 'package:flustars/flustars.dart';
import 'package:osc/event/event.dart';
import 'package:osc/event/event_constant.dart';
import 'package:osc/models/entity/osc_user.dart';
import 'package:osc/utils/string_util.dart';

class OscUserManager {
  // 工厂模式
  factory OscUserManager() =>getInstance();
  static OscUserManager get instance => getInstance();
  static OscUserManager _instance;
//  OscUserEntity user;
  OscUser user;
  int userId;
  String userName;
  String account;
  String password;
  String portrait;
  bool login;
  bool rememberPwd;
  String code;
  String accessToken;
  String refreshToken;
  String tokenType;
  int uid;
  int expiresIn;
  int saveTime;

  List<OscUser> recentUsers;

  OscUserManager._internal() {
    // 初始化
    init();
  }
  static OscUserManager getInstance() {
    if (_instance == null) {
      _instance = new OscUserManager._internal();
    }
    return _instance;
  }

  init(){
    getUserId();
    getUserName();
    getAccount();
    getPortrait();
    isLogin();
    isRememberPwd();
    getRecentUserList();

    getCode();
    getUId();
    getAccessToken();
    getRefreshToken();
    getTokenType();
    getExpiresIn();
    getSaveTime();
  }


  setUser(OscUser user){
    if(user != null) {
      this.user = user;
      setUId(user.id??0);
      setUserId(user.id??0);
      setUserName(user.name);
      setPortrait(user.portrait);
    }
  }

  OscUser getUser(){
    if(user == null){
      user = OscUser(id: getUserId(), name: getUserName(), portrait: getPortrait(),);
    }
    return user;
  }

  setUserId(int userId){
    this.userId = userId;
    setInt("userId", userId);
  }
  int getUserId(){
    if(userId == null || userId == 0){
      userId = getInt("userId");
    }
    return userId;
  }
  setUserName(String userName){
    this.userName = userName;
    setString("userName", userName);
  }
  String getUserName(){
    if(StringUtil.isEmpty(userName)){
      this.userName = getString("userName");
    }
    return userName;
  }
  setAccount(String account){
    this.account = account;
    setString("account", account);
  }
  String getAccount(){
    if(StringUtil.isEmpty(account)){
      this.account = getString("account");
    }
    return account;
  }
  setPortrait(String portrait){
    this.portrait = portrait;
    setString("portrait", portrait);
  }
  String getPortrait(){
    if(StringUtil.isEmpty(portrait)){
      this.portrait = getString("portrait");
    }
    return portrait;
  }
  setPassword(String password){
    this.password = password;
    setString("password", password);
  }
  String getPassword(){
    if(StringUtil.isEmpty(password)){
      this.password = getString("password");
    }
    return password;
  }
  setLogin(bool login){
    this.login = login;
    setBool("login", login);
  }
  bool isLogin(){
    if(login == null || !login){
      login = getBool("login");
    }
    return login;
  }
  setRememberPwd(bool rememberPwd){
    this.rememberPwd = rememberPwd;
    setBool("rememberPwd", rememberPwd);
  }
  bool isRememberPwd(){
    if(rememberPwd == null || !rememberPwd){
      rememberPwd = getBool("rememberPwd");
    }
    return rememberPwd;
  }

  setUId(int uid){
    this.uid = uid;
    setInt("osc_uid", uid);
  }
  int getUId(){
    if(uid == null || uid == 0){
      uid = getInt("osc_uid");
    }
    return uid;
  }
  setExpiresIn(int expiresIn){
    this.expiresIn = expiresIn;
    setInt("osc_expires_in", expiresIn);
    setSaveTime(DateTime.now().millisecondsSinceEpoch);
  }
  int getExpiresIn(){
    if(expiresIn == null || expiresIn == 0){
      expiresIn = getInt("osc_expires_in");
    }
    return expiresIn;
  }
  setSaveTime(int saveTime){
    this.saveTime = saveTime;
    setInt("osc_saveTime", saveTime);
  }
  int getSaveTime(){
    if(saveTime == null || saveTime == 0){
      saveTime = getInt("osc_saveTime");
    }
    return saveTime;
  }
  setCode(String code){
    this.code = code;
    setString("osc_code", code);
  }

  String getCode(){
    if(StringUtil.isEmpty(code)){
      this.code = getString("osc_code");
    }
    return code;
  }
  setAccessToken(String accessToken){
    this.accessToken = accessToken;
    setString("osc_accessToken", accessToken);
  }
  String getAccessToken(){
    if(StringUtil.isEmpty(accessToken)){
      this.accessToken = getString("osc_accessToken");
    }
    return accessToken;
  }
  setRefreshToken(String refreshToken){
    this.refreshToken = refreshToken;
    setString("osc_refreshToken", refreshToken);
  }
  String getRefreshToken(){
    if(StringUtil.isEmpty(refreshToken)){
      this.refreshToken = getString("osc_refreshToken");
    }
    return refreshToken;
  }

  setTokenType(String tokenType){
    this.tokenType = tokenType;
    setString("osc_tokenType", tokenType);
  }
  String getTokenType(){
    if(StringUtil.isEmpty(tokenType)){
      this.tokenType = getString("osc_tokenType");
    }
    return tokenType;
  }

  static const String RECENT_USER = "RECENT_USER";
  addRecentUser(OscUser user){
    if(user != null){
      int index = -1;
      for(OscUser temp in recentUsers){
        if(temp.id == user.id){
          index = recentUsers.indexOf(temp);
          break;
        }
      }
      if(index > -1){
        recentUsers.removeAt(index);
        recentUsers.insert(0, user);
      } else {
        if(recentUsers.length >= 10) {
          recentUsers.removeLast();
        }
        recentUsers.insert(0, user);
      }
      setUserList(recentUsers);
    }
  }

  List<OscUser> getRecentUserList(){
    if(recentUsers == null || recentUsers.isEmpty) {
      recentUsers = SpUtil.getObjList<OscUser>(RECENT_USER, (json) {
        return OscUser.fromJson(json);
      }, defValue: List<OscUser>()
      );
    }
    return recentUsers;
  }
  setUserList(List<OscUser> list){
    SpUtil.putObjectList(RECENT_USER, list);
  }

  logout(){
    eventBus.emit(EventConstant.LOGOUT);
    setUser(null);
    setLogin(false);
    setUserId(0);
    if (!isRememberPwd()) {
      setUserName(null);
      setPassword(null);
    }
    setPortrait(null);
    setRememberPwd(false);

    setCode(null);
    setUId(0);
    setAccessToken(null);
    setRefreshToken(null);
    setExpiresIn(0);
    setSaveTime(0);
    setTokenType(null);
  }


  setString(String key, String value){
    if(StringUtil.isEmpty(key) || StringUtil.isEmpty(value)){
      return;
    }
    SpUtil.putString(key, value);
  }
  setInt(String key, int value){
    if(StringUtil.isEmpty(key) || value < 1){
      return;
    }
    SpUtil.putInt(key, value);
  }
  setBool(String key, bool value){
    if(StringUtil.isEmpty(key)){
      return;
    }
    SpUtil.putBool(key, value);
  }
  String getString(String key){
    String value = SpUtil.getString(key);
    return value;
  }
  int getInt(String key){
    return SpUtil.getInt(key);
  }
  bool getBool(String key){
    return SpUtil.getBool(key);
  }
}